package com.xnx3.banmahuishou.userApplet.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.core.entity.ScoreChangeRecord;
import com.xnx3.banmahuishou.core.entity.SignInRecord;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.vo.BaseVO;


/**
 * 用户签到送积分
 * @author ZXY
 *
 */
@Controller(value="BanmahuishouUserAppletSignInController")
@RequestMapping("/userApplet/signin/")
public class SignInController extends BaseController{
	@Resource
	private SqlService sqlService;


	/**
	 * 判断连续签到
	 * @author zxy
	 */
	@RequestMapping(value = "JudgeSignIn.json",method = {RequestMethod.POST})
	@ResponseBody
	public BaseVO JudgeSignIn(HttpServletRequest request) {
		int id = getUserId() ; //获取用户id
		User user = sqlService.findById(User.class, id);
		if(user == null) {
			return error("请重新扫码进入");
		}
		Integer signTime = DateUtil.timeForUnix10();//签到时间转为时间戳
		//把当前签到的时间戳转为签到日期
		Integer dateSignInteger = Integer.valueOf(DateUtil.intToString(signTime, "yyyyMMdd"));
		//取当前用户在签到记录表内的最后一条记录
		String sql = "SELECT * FROM laji_sign_in_record WHERE userid=" + id + " ORDER BY id DESC LIMIT 1";
		List<SignInRecord> list = sqlService.findBySqlQuery(sql, SignInRecord.class);
		//小于六天的每日增长积分为100 最高积分是600
		int zzJiFen = 100;
		int zgJiFen = 600;
		//判断是不是第一次签到
		if (list.size() == 0 ) {
			//如果是第一次签到 新建一条记录
			SignInRecord signInRecord = new SignInRecord();
			signInRecord.setUserid(id);
			signInRecord.setAddTime(signTime);
			signInRecord.setAddDate(dateSignInteger);
			signInRecord.setSignInCount(1);
			signInRecord.setPrizeNum(zzJiFen);
			sqlService.save(signInRecord);
			//插入签到产生的积分变动记录
			ScoreChangeRecord scoreChangeRecord = new ScoreChangeRecord();
			scoreChangeRecord.setUserid(id);
			scoreChangeRecord.setAddTime(signTime);
			scoreChangeRecord.setChangePoints(zzJiFen);
			scoreChangeRecord.setType(ScoreChangeRecord.TYPE_QIANDAO);
			scoreChangeRecord.setRemarks(DateUtil.intToString(signTime, "yyyy-MM-dd HH:mm:ss") 
					+ user.getNickname() + "第1次签到获得了" + zzJiFen + "积分, 签到记录ID：" + signInRecord.getId());
			scoreChangeRecord.setTitle("签到获得积分");
			sqlService.save(scoreChangeRecord);
			//给用户积分余额加上签到获得的积分
			Integer currency = user.getCurrency() < 0 ? 0 : user.getCurrency();
			user.setCurrency(currency + zzJiFen);
			sqlService.save(user);
		}else {
			//如果不是第一次签到 判断是否连续签到
			for (SignInRecord signIn : list ) {
				//获取最后一条记录的签到日期 
				Integer addDate =  signIn.getAddDate();
				if (addDate.equals(dateSignInteger)) {
					return error("今日已签到");
				}else {
					//日期作差
					int i = dateSignInteger - addDate ;
					//如果差是1 则是连续签到的
					if (i == 1) {
						//判断连续签到次数
						int cishu = signIn.getSignInCount();
						int trueCishu = cishu + 1;
						//如果连续签到次数小于六
						if (cishu < 6) { 
							//新建签到记录
							//将本次签到获得的积分计算出来
							Integer trueJifen = zzJiFen * trueCishu;
							SignInRecord signInRecord = new SignInRecord();
							signInRecord.setUserid(id);
							signInRecord.setAddTime(signTime);
							signInRecord.setAddDate(dateSignInteger);
							signInRecord.setSignInCount(trueCishu);
							signInRecord.setPrizeNum(trueJifen);
							sqlService.save(signInRecord);
							//插入签到产生的积分变动记录
							ScoreChangeRecord scoreChangeRecord = new ScoreChangeRecord();
							scoreChangeRecord.setUserid(id);
							scoreChangeRecord.setAddTime(signTime);
							scoreChangeRecord.setChangePoints(zzJiFen * trueCishu);
							scoreChangeRecord.setType(ScoreChangeRecord.TYPE_QIANDAO);
							scoreChangeRecord.setRemarks(DateUtil.intToString(signTime, "yyyy-MM-dd HH:mm:ss") 
									+ user.getNickname() + "第" + trueCishu + "次签到获得了" + trueJifen + "积分, 签到记录ID：" + signInRecord.getId());
							scoreChangeRecord.setTitle("签到获得积分");
							sqlService.save(scoreChangeRecord);
							//给用户积分余额加上签到获得的积分
							Integer currency = user.getCurrency() < 0 ? 0 : user.getCurrency();
							user.setCurrency(currency + trueJifen);
							sqlService.save(user);
						}else {
							//新建签到记录
							SignInRecord signInRecord = new SignInRecord();
							signInRecord.setUserid(id);
							signInRecord.setAddTime(signTime);
							signInRecord.setAddDate(dateSignInteger);
							signInRecord.setSignInCount(trueCishu);
							signInRecord.setPrizeNum(zgJiFen);
							sqlService.save(signInRecord);
							//插入签到产生的积分变动记录
							ScoreChangeRecord scoreChangeRecord = new ScoreChangeRecord();
							scoreChangeRecord.setUserid(id);
							scoreChangeRecord.setAddTime(signTime);
							scoreChangeRecord.setChangePoints(zgJiFen);
							scoreChangeRecord.setType(ScoreChangeRecord.TYPE_QIANDAO);
							scoreChangeRecord.setRemarks(DateUtil.intToString(signTime, "yyyy-MM-dd HH:mm:ss") 
									+ user.getNickname() + "第" + trueCishu + "次签到获得了" + zgJiFen + "积分, 签到记录ID：" + signInRecord.getId());
							scoreChangeRecord.setTitle("签到获得积分");
							sqlService.save(scoreChangeRecord);
							//给用户积分余额加上签到获得的积分
							Integer currency = user.getCurrency() < 0 ? 0 : user.getCurrency();
							user.setCurrency(currency + zgJiFen);
							sqlService.save(user);
						}
					}else {
						//如果是断签 则从1开始
						SignInRecord signInRecord = new SignInRecord();
						signInRecord.setUserid(id);
						signInRecord.setAddTime(signTime);
						signInRecord.setAddDate(dateSignInteger);
						signInRecord.setSignInCount(1);
						signInRecord.setPrizeNum(zzJiFen);
						sqlService.save(signInRecord);
						//插入签到产生的积分变动记录
						ScoreChangeRecord scoreChangeRecord = new ScoreChangeRecord();
						scoreChangeRecord.setUserid(id);
						scoreChangeRecord.setAddTime(signTime);
						scoreChangeRecord.setChangePoints(zzJiFen);
						scoreChangeRecord.setType(ScoreChangeRecord.TYPE_QIANDAO);
						scoreChangeRecord.setRemarks(DateUtil.intToString(signTime, "yyyy-MM-dd HH:mm:ss") 
								+ user.getNickname() + "第1次签到获得了" + zzJiFen + "积分, 签到记录ID：" + signInRecord.getId());
						scoreChangeRecord.setTitle("签到获得积分");
						sqlService.save(scoreChangeRecord);
						//给用户积分余额加上签到获得的积分
						Integer currency = user.getCurrency() < 0 ? 0 : user.getCurrency();
						user.setCurrency(currency + zzJiFen);
						sqlService.save(user);
					}
				}
			}

		}
		//日志
		ActionLogUtil.insertUpdateDatabase(request, id, "用户端-用户-签到" );
		return success();
	}

}
